﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using One.Models; 

namespace OneSpider.Extensions
{
    public static class ModelExtension
    {
        public static Article ToEssay(this Models.ResultModel.ArticleDetailResult essayDetailResult)
        {
            if (essayDetailResult == null)
            {
                throw new ArgumentNullException(nameof(essayDetailResult));
            }
            else
            {
                //Essay tmp = new Essay()
                //{
                //    Title = essay.HpTitle,
                //    HpMakeTime = essay.HpMakettime,
                //    GuideWord = essay.GuideWord,
                //};
                Article tmp = new Article();
                tmp.AuthIt = essayDetailResult.AuthIt;
                tmp.AuthorIntroduce = essayDetailResult.HpAuthorIntroduce;
                tmp.Content = essayDetailResult.HpContent;
                tmp.SubTitle = essayDetailResult.SubTitle;
                tmp.WebUrl = essayDetailResult.WebUrl;
                tmp.EditorEmail = essayDetailResult.EditorEmail;
                tmp.Tags = new List<Tag>() { };
                essayDetailResult.TagList.ForEach(tag => tmp.Tags.Add(new Tag() { Title = tag.Title }));
                return tmp;
            }
        }

        #region V1
        public static Content ToContent(this OneSpider.Models.ResultModel.ContentResult contentResult)
        {
            if (contentResult == null)
            {
                return null;
            }

            Content content = new Content()
            {
                Title = contentResult.HpTitle,
                ImgUrl = contentResult.HpImgUrl,
                PictureAuthor = $"{contentResult.HpAuthor} | {contentResult.ImageAuthors}",
                TextAuthors = contentResult.TextAuthors,
                PostDate = contentResult.HpMaketime,
                Text = contentResult.HpContent,
                Url = contentResult.WebUrl,
            };

            return content;
        }

        public static List<Content> ToContents(this IEnumerable<OneSpider.Models.ResultModel.ContentResult> contentResults)
        {
            List<Content> contents = new List<Content>();
            if (contentResults == null)
            {
                return contents;
            }

            foreach (var item in contentResults)
            {
                contents.Add(item.ToContent());
            }

            return contents;
        }

        public static Article ToArticle(this OneSpider.Models.ResultModel.ArticleDetailResult articleDetailResult)
        {
            if (articleDetailResult == null)
            {
                return null;
            }

            Article article = new Article()
            {
                AuthIt = articleDetailResult.AuthIt,
                AuthorIntroduce = articleDetailResult.HpAuthorIntroduce,
                Content = articleDetailResult.HpContent,
                SubTitle = articleDetailResult.SubTitle,
                WebUrl = articleDetailResult.WebUrl,
                EditorEmail = articleDetailResult.EditorEmail,
                Title = articleDetailResult.HpTitle,
                HpMakeTime = articleDetailResult.HpMaketime,
                GuideWord = articleDetailResult.GuideWord
            };
             
            article.Tags = new List<Tag>() { };
            articleDetailResult.TagList?.ForEach(tag => article.Tags.Add(new Tag() { Title = tag.Title }));
            return article;
        }

        public static List<Article> ToArticles(this IEnumerable<OneSpider.Models.ResultModel.ArticleDetailResult> articleResults)
        {
            List<Article> articles = new List<Article>();
            if (articleResults == null)
            {
                return articles;
            }

            foreach (var item in articleResults)
            {
                articles.Add(item.ToArticle());
            }

            return articles;
        }

        public static Author ToAuthor(this OneSpider.Models.ResultModel.AuthorResult authorResult)
        {
            if (authorResult == null)
            {
                return null;
            }

            Author author = new Author()
            {
                Name = authorResult.UserName,
                Desc = authorResult.Desc,
                WbName = authorResult.WbName,
                Summary = authorResult.Summary,
                IsSettled = authorResult.IsSettled,
                SettledType = authorResult.SettledType,
                FansTotal = authorResult.FansTotal,
                WebUrl = authorResult.WebUrl
            };

            return author;
        }

        public static List<Answer> ToAnswers(this IEnumerable<OneSpider.Models.ResultModel.AnswerResult> answerResults)
        {
            List<Answer> answers = new List<Answer>();
            if (answerResults == null)
            {
                return answers;
            }

            foreach (var item in answerResults)
            {
                answers.Add(item.ToAnswer());
            }

            return answers;
        }

        public static Answer ToAnswer(this OneSpider.Models.ResultModel.AnswerResult answerResult)
        {
            if (answerResult == null)
            {
                return null;
            }

            Answer answer = new Answer()
            {
                AnswerTitle = answerResult.AnswerTitle,
                QuestionTitle = answerResult.QuestionTitle,
                AnswerSummary = answerResult.AnswerContent,
                MakeTime = answerResult.MakeTime
            };

            if (answerResult.AskerList?.FirstOrDefault() != null)
            {
                answer.AskerName = answerResult.AskerList.First().UserName;
                answer.AskerAvatar = answerResult.AskerList.First().WebUrl;
            }

            if (answerResult.AuthorList?.FirstOrDefault() != null)
            {
                answer.AuthorName = answerResult.AuthorList.First().UserName;
                answer.AuthorAvatar = answerResult.AuthorList.First().WebUrl;
            }

            return answer;
        }

        public static Serial ToSerial(this OneSpider.Models.ResultModel.SerialResult serialResult)
        {
            if (serialResult == null)
            {
                return null;
            }

            Serial serial = new Serial()
            { 
                Title = serialResult.Title,
                Excerpt = serialResult.Excerpt,
                ReadNum = serialResult.ReadNum,
                MakeTime = serialResult.Maketime, 
                SerialList = serialResult.SerialList,
            };

            return serial;
        }
        #endregion

        #region V2

        public static Article ToArticle(this OneSpider.Models.ResultModelV2.ArticleResult articleResult)
        {
            if (articleResult == null)
            {
                return new Article();
            }

            Article article = new Article()
            {
                Title = articleResult.Title,
                GuideWord = articleResult.Forward,
                ImageUrl = articleResult.ImgUrl,
                HpMakeTime = articleResult.PostDate,
                Author = articleResult.Author.ToAuthor()
            };
            article.Tags = new List<Tag>() { };
            articleResult.TagList.ForEach(tag => article.Tags.Add(new Tag() { Title = tag.Title }));
            return article;
        }

        public static List<Article> ToArticles(this IEnumerable<OneSpider.Models.ResultModelV2.ArticleResult> articleResults)
        {
            List<Article> articles = new List<Article>();
            if (articleResults == null)
            {
                return articles;
            }

            foreach (var item in articleResults)
            {
                articles.Add(item.ToArticle());
            }

            return articles;
        }

        public static Author ToAuthor(this OneSpider.Models.ResultModelV2.AuthorResult authorResult)
        {
            if (authorResult == null)
            {
                return new Author();
            }

            Author author = new Author()
            {
                Name = authorResult.UserName,
                Desc = authorResult.Desc,
                WbName = authorResult.WbName,
                Summary = authorResult.Summary,
                IsSettled = authorResult.IsSettled,
                SettledType = authorResult.SettledType,
                FansTotal = authorResult.FansTotal,
                WebUrl = authorResult.WebUrl
            };

            return author;
        }

        public static Music ToMusic(this OneSpider.Models.ResultModelV2.MusicResult musicResult)
        {
            if (musicResult == null)
            {
                return new Music();
            }

            Music music = new Music()
            {
                Title = musicResult.Title,
                Forward = musicResult.Forward,
                ImgUrl = musicResult.ImgUrl,
                PostDate = musicResult.PostDate,
                Author = musicResult.Author.ToAuthor(),
                ShareUrl = musicResult.ShareUrl,
                MusicName = musicResult.MusicName,
                AudioAuthor = musicResult.AudioAuthor,
                AudioAlbum = musicResult.AudioAlbum
            };

            return music;
        }

        public static List<Music> ToMusics(this IEnumerable<OneSpider.Models.ResultModelV2.MusicResult> musicResults)
        {
            List<Music> musics = new List<Music>();
            if (musicResults == null)
            {
                return musics;
            }

            foreach (var item in musicResults)
            {
                musics.Add(item.ToMusic());
            }

            return musics;
        }

        public static Content ToContent(this OneSpider.Models.ResultModelV2.ContentResult contentResult)
        {
            if (contentResult == null)
            {
                return new Content();
            }

            Content content = new Content()
            {
                PostDate = contentResult.Date,
                Title = contentResult.Title,
                Url = contentResult.Url,
                ImgUrl = contentResult.ImgUrl,
                Text = contentResult.Content,
                TextAuthors = contentResult.TextAuthors
            };

            return content;
        }

        public static List<Content> ToContents(this IEnumerable<OneSpider.Models.ResultModelV2.ContentResult> contentResults)
        {
            List<Content> contents = new List<Content>();
            if (contentResults == null)
            {
                return contents;
            }

            foreach (var item in contentResults)
            {
                contents.Add(item.ToContent());
            }

            return contents;
        }

        public static Movie ToMovie(this OneSpider.Models.ResultModelV2.MovieResult movieResult)
        {
            if (movieResult == null)
            {
                return new Movie();
            }

            Movie movie = new Movie()
            {
                Author = movieResult.Author.ToAuthor(),
                Date = movieResult.Date,
                Forward = movieResult.Forward,
                ImgUrl = movieResult.ImgUrl,
                PostDate = movieResult.PostDate,
                SubTitle = movieResult.Subtitle,
                Title = movieResult.Title
            };

            return movie;
        }

        public static List<Movie> ToMovies(this IEnumerable<OneSpider.Models.ResultModelV2.MovieResult> movieResults)
        {
            List<Movie> movies = new List<Movie>();
            if (movieResults == null)
            {
                return movies;
            }

            foreach (var item in movieResults)
            {
                movies.Add(item.ToMovie());
            }

            return movies;
        } 
        #endregion
    }
}
